package com.app.business.web.business;

import com.app.business.calculate.CalculateUtils;
import com.app.business.en.Status;
import com.app.business.model.*;
import com.app.business.service.*;
import com.app.business.utils.CareBuyAreaUtils;
import com.app.system.jpa.PageBean;
import com.app.system.log4j2.LogUtils;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.model.UserInfo;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.WebUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.IntegerUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.AjaxException;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 基础信息控制层
 * Created by drl on 2019/2/11.
 */
@Controller
@RequestMapping("/business/contract/*")
@SuppressWarnings("all")
public class ContractController {

    private Logger logger = LogUtils.getBusinessLogger();
    @Autowired
    private ContractService contractService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private ZsProjectService zsProjectService;
    @Autowired
    private ParameterService parameterService;
    @Autowired
    private ActionHistoryService actionHistoryService;
    @Autowired
    private BuyHousePersonService buyHousePersonService;

    /**
     * 基础数据管理列表(管理员)
     */
    @RequestMapping("adminList")
    @RequiresPermissions("contract:adminList")
    public void adminList() {
    }


    @Autowired
    private HouseService houseService;
    /**
     * 批量执行计算房源对应回迁安置面积
     * @param request
     * @return
     */
    @Transactional
    @RequestMapping("houseAzAreaSave")
    @ResponseBody
    public Response houseAzAreaSave(HttpServletRequest request) {
        List<Contract> contractList = this.contractService.findAll();//得到所有协议信息
        for (Contract contract : contractList) {
            this.houseService.calAzAreaForHouse(contract);
        }
        System.out.println("---批量执行完成------------批量执行完成-------------批量执行完成----------批量执行完成-------批量执行完成--------批量执行完成-----------批量执行完成----------批量执行完成----");
        Response response = new Response();
        response.setCode("0001");
        return response;
    }

    /**
     * 基础数据管理列表
     */
    @RequestMapping("viewList")
    @RequiresPermissions("contract:viewList")
    public void viewList() {
    }

    /**
     * 领导授权管理列表(征收中心)
     */
    @RequestMapping("ldList")
    @RequiresPermissions("contract:ldList")
    public void ldList() {
    }

    /**
     * 产权人提前登记
     */
    @RequestMapping("personDjList")
    public void personList() {
    }

    /**
     * 产权人信息提前登记弹框
     * @param request
     * @param modelMap
     */
    @RequestMapping("personDataForm")
    public void personDataForm(HttpServletRequest request, ModelMap modelMap) {
        int contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null, "未找到相关基础信息！");
        List<BuyHousePerson> buyHousePeopleList = this.buyHousePersonService.getBuyHousePersonListByContractId(contractId);
        modelMap.addAttribute("contract", contract);
        modelMap.addAttribute("buyHousePeopleList", buyHousePeopleList);
    }

    /**
     * 参数配置数据保存
     *
     * @param request
     * @return
     * @throws AjaxException
     */
    @RequestMapping("personDataFormSave")
    @ResponseBody
    public synchronized Response personDataFormSave(HttpServletRequest request) throws AjaxException {
        Response response = this.buyHousePersonService.personDataFormSave(request);
        return response;
    }

    /**
     * 加载基础信息数据
     *
     * @return
     */
    @RequestMapping("loadContractList")
    @ResponseBody
    @RequiresPermissions("contract:loadContractList")
    public Map loadContractList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<Contract> contractPageBean = this.contractService.getContractPageBean();
        List<Contract> contractList = contractPageBean.getDatas();
        Map<String, DataDict> groupMap = this.dataDictService.getMapByParentNameForValue("组别");
        Map<String, DataDict> signMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> checkMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> chooseHouseMap = this.dataDictService.getMapByParentNameForValue("房源签认状态");
        Map<String, DataDict> isAndNoMap = this.dataDictService.getMapByParentNameForValue("是否");
        Map<String,String> getBuyHousePersonNumByContractId = this.buyHousePersonService.getBuyHousePersonNumByContractId();

        if (CollectionUtils.isNotEmpty(contractList)) {
            Map<String, Object> item = null;
            for (Contract contract : contractList) {
                item = null;
                item = Maps.newHashMap();
                item.put("id", contract.getId());
                item.put("zsProjectName", StringUtils.getNotNull(contract.getZsProject().getProjectName()));
                item.put("groupCode", StringUtils.dataDictShow(groupMap.get(contract.getGroupCode())));
                item.put("contractCode", StringUtils.getNotNull(contract.getContractCode()));
                item.put("name", StringUtils.getNotNull(contract.getName()));
                item.put("chooseHouseNum", StringUtils.getNotNull(contract.getChooseHouseNum()));
                item.put("preChooseDate", DateUtils.dateToStringDefault(contract.getPreChooseDate()));
                item.put("limitHouseAreaTotal", BigDecimalUtils.getNotNullStr(contract.getLimitHouseAreaTotal()));
                item.put("chooseHouseStatus", IntegerUtils.getNotNull(contract.getChooseHouseStatus()));
                item.put("chooseHouseStatusStr", StringUtils.dataDictShow(chooseHouseMap.get(IntegerUtils.getNotNull(contract.getChooseHouseStatus()).toString())));
                item.put("azSignStatus", IntegerUtils.getNotNull(contract.getAzSignStatus()));
                item.put("limitStatus", IntegerUtils.getNotNull(contract.getLimitStatus()));
                item.put("limitStatusStr", StringUtils.dataDictShow(isAndNoMap.get(IntegerUtils.getNotNull(contract.getLimitStatus()).toString())));
                item.put("azSignStatusStr", StringUtils.dataDictShow(signMap.get(IntegerUtils.getNotNull(contract.getAzSignStatus()).toString())));
                item.put("financeCheckStatus", IntegerUtils.getNotNull(contract.getFinanceCheckStatus()));
                item.put("financeCheckStatusStr", StringUtils.dataDictShow(checkMap.get(IntegerUtils.getNotNull(contract.getFinanceCheckStatus()).toString())));
                int num = 0;
                if (getBuyHousePersonNumByContractId !=null && getBuyHousePersonNumByContractId.get(contract.getId().toString()) != null) {
                    num = IntegerUtils.stringToInteger(getBuyHousePersonNumByContractId.get(contract.getId().toString()));
                }
                item.put("num",num);
                listData.add(item);
            }
        }
        return LayUiUtils.page(contractPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 管理员基础数据登记
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("adminForm")
    @RequiresPermissions("contract:adminForm")
    public void adminForm(HttpServletRequest request, ModelMap modelMap) {
        UserInfo userInfo = WebUtils.getLoginUserInfo();
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        contract = contract == null ? new Contract() : contract;
        List<String> careBuyAreaNameList = CareBuyAreaUtils.getCareBuyAreaNameList(contract);
        modelMap.addAttribute("careBuyAreaNameList", careBuyAreaNameList);
        modelMap.addAttribute("contract", contract);
    }

    /**
     * 基础数据登记
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("form")
    @RequiresPermissions("contract:form")
    public void form(HttpServletRequest request, ModelMap modelMap) {
        UserInfo userInfo = WebUtils.getLoginUserInfo();
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        contract = contract == null ? new Contract() : contract;
        List<String> careBuyAreaNameList = CareBuyAreaUtils.getCareBuyAreaNameList(contract);
        modelMap.addAttribute("careBuyAreaNameList", careBuyAreaNameList);
        modelMap.addAttribute("contract", contract);
    }

    /**
     * 验证选房顺序号是否已存在
     *
     * @param request
     * @return
     */
    @RequestMapping("validateChooseHouseNum")
    @ResponseBody
    @RequiresPermissions("contract:validateChooseHouseNum")
    public String validateChooseHouseNum(HttpServletRequest request) {
        String chooseHouseNum = ParamUtils.getString(request, "chooseHouseNum", "");
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        int contractId = ParamUtils.getInt(request, "contractId", 0);
        String msg = this.contractService.validateChooseHouseNum(contractId, zsProjectId, chooseHouseNum);//返回的验证信息
        return msg;
    }

    /**
     * 验证协议编号是否已存在
     *
     * @param request
     * @return
     */
    @RequestMapping("validateContractCode")
    @ResponseBody
    @RequiresPermissions("contract:validateContractCode")
    public String validateContractCode(HttpServletRequest request) {
        String contractCode = ParamUtils.getString(request, "contractCode", "");
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        int contractId = ParamUtils.getInt(request, "contractId", 0);
        String msg = this.contractService.validateContractCode(contractId, zsProjectId, contractCode);//返回的验证信息
        return msg;
    }

    /**
     * 家庭基础数据保存
     *
     * @param request
     * @return
     * @throws AjaxException
     */
    @RequestMapping("contractSave")
    @ResponseBody
    @RequiresPermissions("contract:contractSave")
    public synchronized Response contractSave(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Integer zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        Integer parameterId = ParamUtils.getInt(request, "parameterId", 0);

        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
        Parameter parameter = this.parameterService.findOne(parameterId);
        Precondition.checkSqdsArguement(parameter != null, "配置参数为空，若是商业请先选房，选房过后参数人工登记后再保存！");
        Contract contract = this.contractService.findOne(contractId);
        contract = contract == null ? new Contract() : contract;
        if (contract == null) {
            contract = new Contract();
            contract.setPropertyType(zsProject.getPropertyType());
            contract.setCareBuyAreaNameType(zsProject.getCareBuyAreaNameType());
        }
        Precondition.checkSqdsArguement(zsProject != null, "项目不存在！");
        Precondition.checkSqdsArguement(parameter != null, "参数配置不存在！");
        WebUtils.bind(request, contract);
        contract.setPropertyType(zsProject.getPropertyType());
        contract.setCareBuyAreaNameType(zsProject.getCareBuyAreaNameType());
        contract.setZsProject(zsProject);
        contract.setParameter(parameter);
        this.contractService.save(contract);
        CalculateUtils.main(contract.getId());
        return new Response("0001", "保存成功！");
    }

    /**
     * 基础数据查看
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("view")
    @RequiresPermissions("contract:view")
    public void view(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null, "数据错误！");
        Map<String, DataDict> groupMap = this.dataDictService.getMapByParentNameForValue("组别");
        Map<String, DataDict> betweenChooseDateMap = this.dataDictService.getMapByParentNameForValue("安排选房时间段");
        Map<String, DataDict> signMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> chooseHouseStatusMap = this.dataDictService.getMapByParentNameForValue("房源签认状态");
        Map<String, DataDict> financeCheckStatusMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> financeSettleStatusMap = this.dataDictService.getMapByParentNameForValue("财务结清状态");
        List<String> careBuyAreaNameList = CareBuyAreaUtils.getCareBuyAreaNameList(contract);
        modelMap.addAttribute("careBuyAreaNameList", careBuyAreaNameList);
        modelMap.addAttribute("contract", contract);
        modelMap.addAttribute("groupMap", groupMap);
        modelMap.addAttribute("betweenChooseDateMap", betweenChooseDateMap);
        modelMap.addAttribute("signMap", signMap);
        modelMap.addAttribute("chooseHouseStatusMap", chooseHouseStatusMap);
        modelMap.addAttribute("financeCheckStatusMap", financeCheckStatusMap);
        modelMap.addAttribute("financeSettleStatusMap", financeSettleStatusMap);
    }

    /**
     * 批量删除基础数据
     *
     * @param request
     * @return
     */
    @RequestMapping("delContractById")
    @ResponseBody
    @RequiresPermissions("contract:delContractById")
    public synchronized Response delContractById(HttpServletRequest request) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "1111", "数据错误！");
        this.contractService.delete(contract.getId());
        return new Response("0001", "删除成功");
    }

    /**
     * 导入列表
     */
    @RequestMapping("importList")
    @RequiresPermissions("contract:importList")
    public void importList() {
    }

    /**
     * 读取Excel表格
     *
     * @param request
     * @return
     * @throws Exception
     */
    @Transactional
    @RequestMapping("importAll")
    @ResponseBody
    @RequiresPermissions("contract:importAll")
    public Response importAll(HttpServletRequest request) throws Exception {
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
        Precondition.checkAjaxArguement(zsProject != null, "9999", "导入的项目不存在");
        String carLots = request.getParameter("contractArray");
        String[] contractArray = carLots.split("\r\n");

        List<String> contractCodeList = contractService.getContractCodeList(zsProject.getId());

        String contractCode = null, groupCode = null, village = null, name = null, idCard = null, tel = null, chooseHouseNum = null, propertyChangeArea = null, careBuyArea1 = null,
                careBuyArea2 = null, careBuyArea3 = null, careBuyArea4 = null, careBuyArea5 = null, careBuyArea6 = null, careBuyArea7 = null, careBuyArea8 = null,
                tkDate = null, zzgdMoney = null, zxgdMoney = null, resultMoney = null, preChooseDate = null, betweenChooseDate = null, parameterId = null,baoZhangType = null,
                azType = null,drawingCode = null,zqSignDate = null,zqHouseType = null,zgsHouseAzNum = null,zgsHouseAzArea = null,syDmAzNum = null,syDmAzArea = null,syTwoFloorAzArea = null,
                syXzlAzArea = null,syScAzArea = null,carLotYxYdNum = null,carLotYxZhNum = null,house60yxNum = null,house80yxNum = null,house100yxNum = null,house120yxNum= null,house140yxNum = null,house160yxNum= null,
                house180yxNum= null,houseFsOneYxNum = null,houseFsTwoYxNum = null,zqSzLd = null,xcOrJcNum = null,lsAzBzPrice = null,
                lsAzBzZjPrice = null,tcTyBzPrice = null;

//        , isChooseBigHouse = null

        Contract contract = null;

        List<Contract> contractImportList = new ArrayList<>();
        int rowObjLength = 0;//每条数据的列数

        String[] rowObj = null;
        StringBuffer msg = new StringBuffer();
        int index = 0;
        for (String contractInfo : contractArray) {
            rowObj = null;
            rowObj = contractInfo.split("\t");
            rowObjLength = rowObj.length;
            Precondition.checkAjaxArguement(rowObjLength == 44, "0012", "请确认每列都有数据！");
            if (rowObj.length == 0) {
                continue;
            }

            index = 0;
            groupCode = rowObj[index]; //所在工作组
            index++;
            contractCode = rowObj[index]; //协议编号
            index++;
            village = rowObj[index];//村落
            index++;
            name = rowObj[index]; //被征收人
            index++;
            idCard = rowObj[index]; //身份证号
            index++;
            tel = rowObj[index];//联系电话
            index++;
            propertyChangeArea = rowObj[index]; //产权调换面积
            index++;
            careBuyArea1 = rowObj[index]; //可限量照顾购买安置部分


            index++;
            tkDate = rowObj[index]; //腾空日期(yyyy-MM-dd)
            index++;
            zzgdMoney = rowObj[index]; //每月应发周转过渡费
            index++;
            zxgdMoney = rowObj[index]; //每月装修过渡补助费
            index++;
            resultMoney = rowObj[index]; //征收补偿协议中应缴交金额
            index++;
            chooseHouseNum = rowObj[index]; //选房顺序号
            index++;
            preChooseDate = rowObj[index];//安排选房时间
            index++;
            betweenChooseDate = rowObj[index];//安排选房时间段

            index++;
            parameterId = rowObj[index];//使用方案id
            index++;
            baoZhangType = rowObj[index];//保障类型
            index++;
            azType = rowObj[index];//安置方式

            index++;
            drawingCode = rowObj[index];//图纸编号
            index++;
            zqSignDate = rowObj[index];//征迁是签约日期
            index++;
            zqHouseType = rowObj[index];//征迁方式性质
            index++;
            zgsHouseAzNum = rowObj[index];//住改商需安置间数
            index++;
            zgsHouseAzArea= rowObj[index];//住改商需安置面积
            index++;
            syDmAzNum = rowObj[index];//商业店面需安置间数
            index++;
            syDmAzArea = rowObj[index];//商业店面需安置面积
            index++;
            syTwoFloorAzArea = rowObj[index];//商业二层需安置面积
            index++;
            syXzlAzArea = rowObj[index];//商业写字楼需安置面积
            index++;
            syScAzArea = rowObj[index];//商业商场需安置面积
            index++;
            carLotYxYdNum = rowObj[index];//车位意向预定数量
            index++;
            carLotYxZhNum = rowObj[index];//车位意向置换数量
            index++;
            house60yxNum = rowObj[index];//房屋60型数量
            index++;
            house80yxNum = rowObj[index];//房屋80型数量
            index++;
            house100yxNum = rowObj[index];//房屋100型数量
            index++;
            house120yxNum = rowObj[index];//房屋120型数量
            index++;
            house140yxNum = rowObj[index];//房屋140型数量
            index++;
            house160yxNum = rowObj[index];//房屋160型数量
            index++;
            house180yxNum = rowObj[index];//房屋180型数量
            index++;
            houseFsOneYxNum = rowObj[index];//房屋复式1型数量
            index++;
            houseFsTwoYxNum = rowObj[index];//房屋复式2型数量
            index++;
            zqSzLd = rowObj[index];//所在路段
            index++;
            xcOrJcNum = rowObj[index];//析产继承份数
            index++;
            lsAzBzPrice = rowObj[index];//临时安置补偿标准
            index++;
            lsAzBzZjPrice = rowObj[index];//临时安置补偿(申请增加部分)标准
            index++;
            tcTyBzPrice = rowObj[index];//停业补


            //只能导入编号不存在
            if (contractCodeList.contains(contractCode)) {
                //已经存在的数据输出到页面
                msg.append("<br/>" + contractInfo + ",");
                continue;
            }

            contract = new Contract();

            contract.setZsProject(zsProject);
            contract.setGroupCode(groupCode);
            contract.setVillage(village);
            contract.setContractCode(contractCode);
            contract.setName(name);
            contract.setIdCard(idCard);
            contract.setTel(tel);

            contract.setPropertyChangeArea(BigDecimalUtils.stringToBigDecimal(propertyChangeArea));
            contract.setCareBuyArea1(BigDecimalUtils.stringToBigDecimal(careBuyArea1));
            contract.setCareBuyArea2(BigDecimalUtils.stringToBigDecimal(careBuyArea2));
            contract.setCareBuyArea3(BigDecimalUtils.stringToBigDecimal(careBuyArea3));
            contract.setCareBuyArea4(BigDecimalUtils.stringToBigDecimal(careBuyArea4));
            contract.setCareBuyArea5(BigDecimalUtils.stringToBigDecimal(careBuyArea5));
            contract.setCareBuyArea6(BigDecimalUtils.stringToBigDecimal(careBuyArea6));
            contract.setCareBuyArea7(BigDecimalUtils.stringToBigDecimal(careBuyArea7));
            contract.setCareBuyArea8(BigDecimalUtils.stringToBigDecimal(careBuyArea8));

            contract.setTkDate(DateUtils.stringToDateDefault(tkDate));
            contract.setZzgdMoney(BigDecimalUtils.stringToBigDecimal(zzgdMoney));
            contract.setZxgdMoney(BigDecimalUtils.stringToBigDecimal(zxgdMoney));
            contract.setResultMoney(BigDecimalUtils.stringToBigDecimal(resultMoney));
            contract.setChooseHouseNum(chooseHouseNum);
            contract.setPreChooseDate(DateUtils.stringToDateDefault(preChooseDate));
            contract.setBetweenChooseDate(IntegerUtils.stringToInteger(betweenChooseDate));
            contract.setParameter(this.parameterService.findOne(IntegerUtils.stringToInteger(parameterId != null ? parameterId : "2")));
            contract.setLimitStatus(1);

            contract.setAzType(azType);
            contract.setDrawingCode(drawingCode);
            contract.setZqSignDate(DateUtils.stringToDateDefault(zqSignDate));
            contract.setZqHouseType(zqHouseType);
            contract.setZgsHouseAzNum(IntegerUtils.stringToInteger(zgsHouseAzNum));
            contract.setZgsHouseAzArea(BigDecimalUtils.stringToBigDecimal(zgsHouseAzArea));
            contract.setSyDmAzNum(IntegerUtils.stringToInteger(syDmAzNum));
            contract.setSyDmAzArea(BigDecimalUtils.stringToBigDecimal(syDmAzArea));
            contract.setSyTwoFloorAzArea(BigDecimalUtils.stringToBigDecimal(syTwoFloorAzArea));
            contract.setSyXzlAzArea(BigDecimalUtils.stringToBigDecimal(syXzlAzArea));
            contract.setSyScAzArea(BigDecimalUtils.stringToBigDecimal(syScAzArea));
            contract.setCarLotYxYdNum(IntegerUtils.stringToInteger(carLotYxYdNum));
            contract.setCarLotYxZhNum(IntegerUtils.stringToInteger(carLotYxZhNum));
            contract.setHouse60YxNum(IntegerUtils.stringToInteger(house60yxNum));
            contract.setHouse80YxNum(IntegerUtils.stringToInteger(house80yxNum));
            contract.setHouse100YxNum(IntegerUtils.stringToInteger(house100yxNum));
            contract.setHouse120YxNum(IntegerUtils.stringToInteger(house120yxNum));
            contract.setHouse140YxNum(IntegerUtils.stringToInteger(house140yxNum));
            contract.setHouse160YxNum(IntegerUtils.stringToInteger(house160yxNum));
            contract.setHouse180YxNum(IntegerUtils.stringToInteger(house180yxNum));
            contract.setHouseFsOneYxNum(IntegerUtils.stringToInteger(houseFsOneYxNum));
            contract.setHouseFsTwoYxNum(IntegerUtils.stringToInteger(houseFsTwoYxNum));
            contract.setZqSzLd(zqSzLd);
            contract.setXcOrJcNum(IntegerUtils.stringToInteger(xcOrJcNum));
            contract.setLsAzBzPrice(BigDecimalUtils.stringToBigDecimal(lsAzBzPrice));
            contract.setLsAzBzZjPrice(BigDecimalUtils.stringToBigDecimal(lsAzBzZjPrice));
            contract.setTcTyBzPrice(BigDecimalUtils.stringToBigDecimal(tcTyBzPrice));

            contract.setTextCode(contract.getZsProject().getProjectCode());
            contract.setBaoZhangType(baoZhangType);
            //物业减免类型和照顾购买类型名称默认跟随项目的
            contract.setPropertyType(zsProject.getPropertyType());
            contract.setCareBuyAreaNameType(zsProject.getCareBuyAreaNameType());

            contractImportList.add(contract);
        }

        this.contractService.batchSave(contractImportList);
        String successInfo = "插入" + contractImportList.size() + "条数据;";
        msg = msg.length() == 0 ? msg : msg.replace(0, msg.length(), msg.toString()).append("<br/>数据已存在无法导入");
        Response response = new Response("0001", successInfo + msg.toString());
        return response;
    }

    /**
     * 选房安置告知书、选房通知单、选房安置资格审查表打印
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("print")
    @RequiresPermissions("contract:print")
    public void print(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "1111", "数据错误！");
        modelMap.addAttribute("contract", contract);
    }

    /**
     * 批量打印界面
     */
    @RequestMapping("batchPrint")
    @RequiresPermissions("contract:batchPrint")
    public void batchPrint(ModelMap modelMap) {
        //通过用户id获取所分配的征收项目list
        List<ZsProject> zsProjectList = this.zsProjectService.zsProjectList();
        modelMap.addAttribute("zsProjectList", zsProjectList);

    }

    /**
     * 选房通知单批量打印
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("batchChooseNoticePrint")
    @RequiresPermissions("contract:batchChooseNoticePrint")
    public void batchChooseNoticePrint(HttpServletRequest request, ModelMap modelMap) {
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        String preChooseDate = ParamUtils.getString(request, "preChooseDate", "");
        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
        Precondition.checkSqdsArguement(zsProject != null, "项目不存在！");

        modelMap.addAttribute("zsProject", zsProject);
        modelMap.addAttribute("preChooseDate", preChooseDate);

    }

    /**
     * 选房安置告知书批量打印
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("batchGzBillPrint")
    @RequiresPermissions("contract:batchGzBillPrint")
    public void batchGzBillPrint(HttpServletRequest request, ModelMap modelMap) {
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        String preChooseDate = ParamUtils.getString(request, "preChooseDate", "");
        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
        Precondition.checkSqdsArguement(zsProject != null, "项目不存在！");

        modelMap.addAttribute("zsProject", zsProject);
        modelMap.addAttribute("preChooseDate", preChooseDate);

    }

    /**
     * 选房安置资格审查表批量打印
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("batchReviewTablePrint")
    @RequiresPermissions("contract:batchReviewTablePrint")
    public void batchReviewTablePrint(HttpServletRequest request, ModelMap modelMap) {
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        String preChooseDate = ParamUtils.getString(request, "preChooseDate", "");
        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
        Precondition.checkSqdsArguement(zsProject != null, "项目不存在！");

        modelMap.addAttribute("zsProject", zsProject);
        modelMap.addAttribute("preChooseDate", preChooseDate);

    }

    /**
     * 安置流程卡批量打印
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("batchProcessPrint")
    @RequiresPermissions("contract:batchProcessPrint")
    public void batchProcessPrint(HttpServletRequest request, ModelMap modelMap) {
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        String preChooseDate = ParamUtils.getString(request, "preChooseDate", "");
        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
        Precondition.checkSqdsArguement(zsProject != null, "项目不存在！");

        modelMap.addAttribute("zsProject", zsProject);
        modelMap.addAttribute("preChooseDate", preChooseDate);

    }

    /**
     * 查询某项目下所有的选房安排时间（去重的）
     *
     * @param request
     * @return
     */
    @RequestMapping("loadChooseDate")
    @ResponseBody
    @RequiresPermissions("contract:loadChooseDate")
    public String loadChooseDate(HttpServletRequest request) {
        Integer zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        //1：打印的选房通知书 2:打印的资格审查表
        Integer type = ParamUtils.getInt(request, "type", 0);
        //通过征收项目id 得到选房安排时间里list
        List dateList = contractService.getPreChooseDateByZsProjectId(zsProjectId);

        StringBuffer html = new StringBuffer("");
        if (CollectionUtils.isNotEmpty(dateList)) {
            Object date = null;
            for (Object data : dateList) {
                Object[] da = (Object[]) data;
                date = DateUtils.dateToStringDefault(DateUtils.stringToDateDefault(String.valueOf(da[0])));
                if (type == 1) {
                    html.append("<input type='checkbox' name='chooseDate' class='layui-input chooseDate'  lay-skin='primary' ");
                } else if (type == 2) {
                    html.append("<input type='checkbox' name='preChooseDate' class='layui-input preChooseDate'  lay-skin='primary' ");
                }
                html.append(" title= '" + date + "：[" + da[1] + "人]' ");
                html.append(" value = '" + date + "' ");
                html.append("/>");
            }
        } else {
            html.append("请先设置选房安排时间！");
        }
        return html.toString();
    }

    /**
     * 统计打印数量
     *
     * @param request
     * @return
     */
    @RequestMapping("countContractData")
    @ResponseBody
    @RequiresPermissions("contract:countContractData")
    public Response countContractData(HttpServletRequest request) {
        Integer size = this.contractService.countContractSize(request);
        return new Response("0001", "总计打印" + size + "份单据！");
    }

    /**
     * 设置领导授权状态（不可撤销）
     *
     * @param request
     */
    @RequestMapping("setLdStatus")
    @ResponseBody
    @RequiresPermissions("contract:setLdStatus")
    public synchronized Response setLdStatus(HttpServletRequest request) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "1111", "数据错误！");
        Precondition.checkAjaxArguement(contract.getLimitStatus().equals(Status.YES.getIndex()), "2222", "该户已授权无需授权！");

        contract.setLimitStatus(Status.NO.getIndex());
        this.actionHistoryService.saveActionHistoryByContractId(contract.getId(), "设置为可选房", "授权管理");
        this.contractService.save(contract);
        return new Response("0001", "操作成功");
    }

    /**
     * 根据协议ID新增一条基础信息
     *
     * @param request
     */
    @RequestMapping("setNewContractForC")
    @ResponseBody
    @RequiresPermissions("contract:setNewContractForC")
    public synchronized Response setDaffStatus(HttpServletRequest request) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        String str = null;
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "1111", "数据错误！");

        Contract newContract = new Contract();
        newContract.setZsProject(contract.getZsProject());
        newContract.setGroupCode(contract.getGroupCode());
        newContract.setContractCode(contract.getContractCode() + "-C");
        newContract.setTel(contract.getTel());
        newContract.setIdCard(contract.getIdCard());
        newContract.setName(contract.getName());
        newContract.setPropertyChangeArea(new BigDecimal("0"));
        newContract.setPreChooseDate(new Date());
        newContract.setBetweenChooseDate(1);
        newContract.setChooseHouseNum(contract.getChooseHouseNum() + "-C");
        newContract.setParameter(contract.getParameter());
        newContract.setTkDate(contract.getTkDate());
        newContract.setCareBuyAreaNameType(contract.getCareBuyAreaNameType());
        newContract.setPropertyType(contract.getPropertyType());

        this.contractService.save(newContract);
        this.actionHistoryService.saveActionHistoryByContractId(contract.getId(), str, "通过点击增加复制数据协议，新ID为" + contract.getId());

        return new Response("0001", "操作成功");
    }
}
